
//  shashka.mm
//  iminib3d
//
//  Created by Huly-Gun Megatron on 10/3/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

//#include "iminib3d.h"
//#include "game.h"

#import "xors3d.h"
#import "game_const.h"
#include "shashka.h"
#import "vars.h"


//#import "game.h"


Shashka::Shashka(int _type, float x, float y,long _ent)
{
	entity=xCopyEntity(_ent,0);
	flag = 0;
	vx = vy = 0;
	stype = _type;
	xRotateEntity(entity, 90, 0, 0,0);
	xEntityPickMode(entity,2);
	xPositionEntity(entity,x, y, 0.5, 1);
	xShowEntity(entity);
	xUpdateNormals(entity);
	xEntityType(entity,1,true);
	xEntityRadius(entity, 1.15,1.15);
	
	static int n=1;
	num=n++;
}

Shashka::~Shashka()
{
	xFreeEntity(entity);
	//NSLog(@"~Shashka");
}

bool Shashka::activate(bool act)
{
	bool old_state = flag==1;
	if (act)
	{
		flag = 1;
	}
	else
	{
		flag = 0;
		vx = vy = 0;
	}
	return old_state;
}


float Shashka::getDistance(Shashka* sh){
	float dx = fabs(xEntityX(entity,true) - xEntityX(sh->entity,true));
	float dy = fabs(xEntityY(entity,true) - xEntityY(sh->entity,true));
	return sqrt(dx*dx+dy*dy);
}

void Shashka::normalyzeMove(float f)
{
	float r = getRMove();
	if (r > f)
	{
		//setXMove(getXMove()*f/r);
		vx = vx*f/r;
		//setYMove(getYMove()*f/r);
		vy = vy*f/r;
	}
}

bool Shashka::stepMove()
{
	double k;
	
	if (diffic<3){
		k = 0.25; //0.4
	}else {
		k= 0.4;
	}

	// выходим, если ходить не надо
	if (flag==0) return false;
	
	if (getRMove() < k)
	{ // остановка
		activate(false);
	}
	else{  

		xTranslateEntity(entity, vx/4, vy/4, 0);
		double nR = getRMove()-k/4;
		normalyzeMove(nR);
	}
	return flag==0;
}